package com.xiaozhen.system.controller;

import com.xiaozhen.security.controller.BaseController;
import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.common.core.utils.StringUtils;
import com.xiaozhen.common.core.domain.system.SysDictData;
import com.xiaozhen.system.service.SysDictDataService;
import com.xiaozhen.system.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/8/5 9:40
 * @description:
 */
@Api(tags = "数据字典信息")
@RestController
@RequestMapping("/sysDict/data")
public class SysDictDataController extends BaseController {
    @Autowired
    private SysDictDataService dictDataService;

    @Autowired
    private SysDictTypeService dictTypeService;

    @ApiOperation("根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data)) {
            data = new ArrayList<>();
        }
        return AjaxResult.success(data);
    }

    @ApiOperation("新增字典数据信息")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody SysDictData dictData) {
        dictData.setCreateBy(getCurrentUserId());
        dictData.setCreateTime(LocalDateTime.now());
        return toAjax(dictDataService.insertDictData(dictData));
    }

    @ApiOperation("修改字典数据信息")
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody SysDictData dictData) {
        dictData.setUpdateBy(getCurrentUserId());
        dictData.setUpdateTime(LocalDateTime.now());
        return toAjax(dictDataService.updateDictData(dictData));
    }

    @ApiOperation("删除字典数据信息")
    @DeleteMapping("/{dictCode}")
    public AjaxResult remove(@PathVariable Long dictCode) {
        return toAjax(dictDataService.deleteDictDataById(dictCode));
    }
}
